package com.sdtv.basemodule.utils;

/**
 * @ProjectName: HaiKanTV_Android
 * @Package: com.sdtv.basemodule.utils
 * @ClassName: ToolBarHelper
 * @Description: 该类是用来处理导航栏的基础类
 * 主要作用是用来拼接需要导航的页面  页面格式为 toolbar + layout
 * 使用公共的toolbar来处理导航
 * @Author: 小郭同学
 * @CreateDate: 2019-10-10 15:56
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.BarUtils;
import com.sdtv.basemodule.R;
import com.sdtv.basemodule.listeners.ToolBarClickListener;


public class ToolBarHelper {

    // 上下文环境
    private Context mContext;
    // 视图构造器
    private LayoutInflater mInflater;

    public FrameLayout getRootLayout() {
        return rootLayout;
    }

    // 页面根视图
    private FrameLayout rootLayout;
    //用户希望显示的view
    private View mUserView;
    // toolbar的基本控件绑定
    private View bottomLine;
    private ImageView tbBack;
    private TextView tbTitleTextView;
    private TextView tbRightText;
    // 按钮点击事件监听
    private ToolBarClickListener listener;

    /**
     * 构造器
     *
     * @param context
     * @param layoutId
     * @param callback 点击事件的监听
     */
    public ToolBarHelper(Context context, int layoutId, ToolBarClickListener callback) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        this.listener = callback;
        // 创建页面跟视图
        initRootView();
        //设置toolbar(
        initToolBar();
        // 设置用户页面视图
        initUserView(layoutId);
    }


    /**
     * 设置页面跟视图
     */
    private void initRootView() {
        // 创建一个FrameLayout,该FrameLayout作为视图的跟视图，
        // 该视图下由  toolbar + layout(用户需要展示的content)组成
        rootLayout = new FrameLayout(mContext);
        FrameLayout.LayoutParams rootLayoutParams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        rootLayout.setLayoutParams(rootLayoutParams);
    }

    /**
     * 设置页面toolbar 并添加到根视图中
     */
    private void initToolBar() {
        // 获取toolbar
        View toolBarView = mInflater.inflate(R.layout.layout_toolbar, rootLayout);
        tbBack = toolBarView.findViewById(R.id.tb_back);
        tbTitleTextView = toolBarView.findViewById(R.id.tb_titleTextView);
        tbRightText = toolBarView.findViewById(R.id.tb_rightText);
        bottomLine = toolBarView.findViewById(R.id.bottom_line);
        //点击事件的监听
        tbBack.setOnClickListener(clickProxy);
        // 右侧按钮
        tbRightText.setOnClickListener(clickProxy);
    }


    /**
     * 点击事件设置
     * 避免连续点击
     */
    private ClickProxy clickProxy = new ClickProxy(v -> {
        // 返回按钮点击事件  --
        if (null != listener) {
            listener.tabClickListener(v);
        }
    });


    /**
     * 设置页面视图
     */
    private void initUserView(int layoutId) {
        mUserView = mInflater.inflate(layoutId, null);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mUserView.setLayoutParams(lp);
        lp.topMargin = BarUtils.getStatusBarHeight();
        rootLayout.addView(mUserView, lp);
    }

    /**
     * 设置返回按钮的显示与隐藏
     *
     * @param isShow
     */
    public void showBackButton(boolean isShow) {
        if (isShow) {
            tbBack.setVisibility(View.VISIBLE);
        } else {
            tbBack.setVisibility(View.GONE);
        }
    }

    /**
     * 设置右侧按钮的显示与隐藏
     *
     * @param isShow
     */
    public void showRightTab(boolean isShow) {
        if (isShow) {
            tbRightText.setVisibility(View.VISIBLE);
        } else {
            tbRightText.setVisibility(View.GONE);
        }
    }

    /**
     * 设置右侧按钮的内容
     */
    public void setRightTabContent(String tabName) {
        if (!TextUtils.isEmpty(tabName)) {
            tbRightText.setText(tabName);
        }
    }

}
